<?php
class posicion extends objeto{
	var $images=array();
	var $id;
	var $_tabla="posiciones";
	public function posicion($id){
		$this->id=$id;
	}
	public function getImage($index=0){
		
		if(!count($this->images)){
			$this->_loadImages();
		}
		
		return $this->images[$index];
	}
	public function getURL(){
		
	}
	private function _loadImages(){
		global $config,$core;
		$db=&$core->getDB();
    	$con=&$db->getConexion();
		 include_once $config->paths["core/class"].'imagen.class.php';
		$q="select `id` from `imagenes` where `id_posicion`='".mysql_real_escape_string($this->id)."' order by `orden`";
		$r=mysql_query($q);
		$this->images=array();
		while($i=mysql_fetch_array($r,MYSQL_ASSOC)){
			$this->images[]=new imagen($i["id"]);
		}
	}
	public function existe(){
		global $core;
		$db=&$core->getDB();
    	$con=&$db->getConexion();
	
		$q="select `id` from `posiciones` where `id`='".mysql_real_escape_string($this->id)."' limit 1";
		$r=mysql_query($q);
		return mysql_num_rows($r);
	}
	public function getRelated(){
		global $core;
		$db=&$core->getDB();
    	$con=&$db->getConexion();
		$q="select  `p`.`id` as 'id'
			from `posiciones` as `p` 
			where `p`.`id`!='".mysql_real_escape_string($this->id)."' ";
		$r=mysql_query($q);
		$relacionados=array();
		while($i=mysql_fetch_array($r,MYSQL_ASSOC)){
			$relacionados[]=new posicion($i["id"]);
			}
		return $relacionados;
	}
	public function getThumbNail(){
		$q="select `i`.`path` as 'path'
			from `imagen360` as `i`
			where  `i`.`posicion`='".mysql_real_escape_string($this->id)."'
			limit 1";
		$r=mysql_query($q);
		$i=mysql_fetch_array($r,MYSQL_ASSOC);
		return "cache/1100/0/700/200/1355/400/".$i["path"];
	}
	public function getLink(){
		$link="ver/".$this->id."/".urlencode($this->get("titulo"));
		return $link;
	}
	public function getAll($relacionados=false){
		global $core;
		$db=&$core->getDB();
    	$con=&$db->getConexion();
		$q="select `p`.`id` as 'id',
				`p`.`lat` as 'lat',
				`p`.`lon` as 'lng',
				`p`.`titulo` as 'titulo',
				`p`.`descripcion` as 'descripcion',
				`p`.`localidad` as 'localidad',
				`p`.`precio_from` as 'precio_from',
				`p`.`precio_to` as 'precio_to',
				`i`.`path` as 'imagen360'
			from `posiciones` as `p`
				left join `imagen360` as `i` on `i`.`posicion`=`p`.`id` 
			where `p`.`id`='".mysql_real_escape_string($this->id)."' 
			limit 1";
		$r=mysql_query($q);
		$posicion=mysql_fetch_array($r,MYSQL_ASSOC);
		$posicion["thumbnail"]=str_replace("galeria/imagenes/360/","galeria/imagenes/360thumbs/",$posicion["imagen360"]);
		$q="select `s`.`id` as 'id',
				`s`.`nombre` as 'nombre',
				`s`.`descripcion` as 'descripcion',
				`s`.`icon_on` as 'icon_on',
				`s`.`icon_off` as 'icon_off'
			from `posicion_servicio` as `ps`
			left join `servicios` as `s` on `ps`.`servicio`=`s`.`id`
			where `ps`.`posicion`='".mysql_real_escape_string($this->id)."'";
		$r=mysql_query($q);
		
		if(mysql_num_rows($r)){
			$posicion["servicios"]=array();
			while($i=mysql_fetch_array($r,MYSQL_ASSOC)){
				$posicion["servicios"]["servicio_".$i["id"]]=$i;
			}
		}
		if($relacionados){
			$rel=$this->getRelated();
			if(count($rel)){
				$posicion["relacionados"]=array();
				foreach($rel as $r){
					$posicion["relacionados"]["posicion_".$r->id]=$r->getAll();
				}
			}
		}
		$q="select `id`,`ruta` from `imagenes` where `id_posicion`='".mysql_real_escape_string($this->id)."'";
		$r=mysql_query($q);
		if(mysql_num_rows($r)){
			$posicion["galeria"]=array();
			while($i=mysql_fetch_array($r,MYSQL_ASSOC)){
				$posicion["galeria"]["imagen_".$i["id"]]=$i;
			}
		}
		return $posicion;
	}
}
